﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using static System.Net.Mime.MediaTypeNames;
using System.Net;

namespace AuSys.Services.App.ActionFilters
{
    public class DevOnlyFilter(
        IWebHostEnvironment env)
        : IAsyncResourceFilter
    {
        public async Task OnResourceExecutionAsync(
            ResourceExecutingContext context, ResourceExecutionDelegate next)
        {
            if (!env.IsDevelopment())
            {
                context.Result = new ContentResult()
                {
                    Content = "仅在开发环境可用",
                    ContentType = Text.Plain,
                    StatusCode = (int)HttpStatusCode.Forbidden
                };
            }
            else
                await next();
        }
    }

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
    public class DevOnlyAttribute
        : TypeFilterAttribute<DevOnlyFilter>
    {

    }
}
